﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Com_Orion_Test_DLL;
using System.Diagnostics;

namespace Com_Orion_TestRUNTool
{
    public partial class Form1 : Form
    {
        private xmlCtrl XMLCTRL;
        private List<DateTime> KickDateList = new List<DateTime>();
        public Form1()
        {
            InitializeComponent();
        }

        private void xmlOpenBtn_Click(object sender, EventArgs e)
        {
            OpenFileDialog xmlOpenDlg = new OpenFileDialog();
            xmlOpenDlg.FileName = "setting.xml";
            xmlOpenDlg.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            xmlOpenDlg.Filter ="XMLファイル(*.xml)|*.xml|すべてのファイル(*.*)|*.*";
            if (xmlOpenDlg.ShowDialog() == DialogResult.OK)
            {
                xmlPath_TBox.Text = xmlOpenDlg.FileName;
            }
        }

        private void RunBtn_Click(object sender, EventArgs e)
        {
            int timer;
            if (!Int32.TryParse(waitTimer_TBox.Text, out timer))
                timer = 300;
            XMLCTRL = new xmlCtrl(xmlPath_TBox.Text);
            foreach (TestProgramInfo TP in XMLCTRL.ArrayTP)
            {
                TP.Run();
                KickDateList.Add(DateTime.Now);
                if (TP != XMLCTRL.ArrayTP.Last())
                    //Thread.Sleep(timer);
                    wait(DateTime.Now, timer);
            }
            writeDateLog();
            MessageBox.Show("全プログラムを実行しました。");
        }
        private void wait(DateTime now,int timer)
        {
            do
            {
                Thread.Sleep(5);
                DateTime dt = DateTime.Now;
            } while (DateTime.Now < now.AddMilliseconds(timer));
        }
        private void writeDateLog()
        {
            //DefaultTraceListenerオブジェクトを取得
            DefaultTraceListener drl;
            drl = (DefaultTraceListener) Trace.Listeners["Default"];
            //LogFileNameを変更する
            drl.LogFileName = ".\\test.txt";
            foreach (DateTime dt in KickDateList)
            {
                Trace.WriteLine(dt.ToString("MM/dd/yyyy hh:mm:ss.fff"));
            }

        }
        
        
    }
}
